import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todo/models/models.dart';
import 'package:todo/widgets/widgets.dart';

class TodosPage extends StatelessWidget {
  void onValueChange(context, value, type, index) {
    var todo = Provider.of<NTodoList>(context);
    todo.finished(type, index);
  }

  Widget _todoWidget(context, dynamic todo, int i, NTheme theme) {
    return Padding(
      padding: EdgeInsets.only(bottom: 40),
      child: TodoListBox(
        minHeight: 100,
        maxHeight: 200,
        titleColor: theme.todoListBoxTitle[i],
        title: Text(
          todo.name,
          style: TextStyle(fontSize: 12, color: Colors.white),
        ),
        itemCount: todo.data.length,
        itemBuilder: (context, index) => TodoListviewItem(
          lable: todo.data[index],
          color: theme.todoListBoxTitle[i],
          onValueChanged: (value) =>
              this.onValueChange(context, value, i, index),
        ),
      ),
    );
  }

  List<Widget> _todoWidgets(context) {
    var theme = Provider.of<NTheme>(context);
    var todo = Provider.of<NTodoList>(context);
    return todo.list
        .asMap()
        .map((i, v) => MapEntry(i, _todoWidget(context, v, i, theme)))
        .values
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return TodoPage(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(top: 30, bottom: 30),
            child: TodoFaceLoginBox(
//              nickName: "",
              username: "yenole",
              avaterUrl: NetworkImage(
                "https://avatars3.githubusercontent.com/u/6938587?s=460&v=4",
              ),
              onLogin: () => debugPrint("登录"),
            ),
          ),
          Column(
            children: _todoWidgets(context),
          )
        ],
      ),
    );
  }
}
